import 'package:flutter/material.dart';
import 'package:kasax/commons/commons.dart';

/// simpledialog 示例
class SimpleDialogDemo extends StatefulWidget {
  const SimpleDialogDemo({Key? key}) : super(key: key);

  @override
  _SimpleDialogDemoState createState() => _SimpleDialogDemoState();
}

enum ChooseType { A, B, C }

class _SimpleDialogDemoState extends State<SimpleDialogDemo> {
  String _selectValue = "Northing";

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Padding(
        padding: EdgeInsets.all(16),
        child: SingleChildScrollView(
          child: Container(
            child: Column(
              children: [
                Container(
                  child: Text(
                    "您选择了: $_selectValue",
                    style: TextStyle(fontSize: 18),
                  ),
                ),
                SizedBox(
                  height: 15,
                ),
                ListView(shrinkWrap: true, children: [
                  buildCircleButton(
                    "显示弹窗",
                    () {
                      showSimpleDialog();
                    },
                  ),
                  buildCircleButton(
                    "显示弹窗-2，输出值",
                    () {
                      showSimpleDialog2();
                    },
                  )
                ])
              ],
            ),
          ),
        ),
      ),
    );
  }

  showSimpleDialog() {
    showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: const Text("消息通知"),
            children: [
              SimpleDialogOption(
                child: Text("Option-A"),
                onPressed: () {
                  // Navigator.of(context).pop();
                  Navigator.pop(context);
                },
              ),
              SimpleDialogOption(
                child: Text("Option-B"),
                onPressed: () {
                  // Navigator.of(context).pop();
                  Navigator.pop(context);
                },
              ),
              SimpleDialogOption(
                child: Text("Option-C"),
                onPressed: () {
                  // Navigator.of(context).pop();
                  Navigator.pop(context);
                },
              ),
            ],
          );
        });
  }

  void showSimpleDialog2() {
    execShowDialog2();
  }

  Future execShowDialog2() async {
    final option = await showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: Text("区域选择"),
            children: [
              SimpleDialogOption(
                child: Text("北京"),
                onPressed: () {
                  // 回传值
                  Navigator.pop(context, ChooseType.A);
                },
              ),
              SimpleDialogOption(
                child: Text("上海"),
                onPressed: () {
                  Navigator.pop(context, ChooseType.B);
                },
              ),
              SimpleDialogOption(
                child: Text("广东"),
                onPressed: () {
                  Navigator.pop(context, ChooseType.C);
                },
              )
            ],
          );
        });
    String selectV = _selectValue;
    switch (option) {
      case ChooseType.A:
        selectV = "北京";
        break;
      case ChooseType.B:
        selectV = "上海";
        break;
      case ChooseType.C:
        selectV = "深圳";
        break;
    }
    setState(() {
      _selectValue = selectV;
    });
  }
}
